package com.sharding.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.sharding.entity.Account;
import com.sharding.entity.User;
import com.sharding.mapper.AccountRepository;
import com.sharding.mapper.UserRepository;
import com.sharding.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author sw
 * @create 2023-05-04 16:51
 */

@RestController
@RequestMapping("/user")
@ResponseBody
public class UserController {

    @Autowired
    UserRepository userRepository;

    @GetMapping("/list")
    public String getPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        Page<User> accountPage = userRepository.selectPage(new Page<>(page, pageSize), queryWrapper);

        return JSON.toJSONString(accountPage);
    }

    @GetMapping("/total")
    public Long getTotal() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        return userRepository.selectCount(queryWrapper);
    }

    @GetMapping("/left")
    public String getLeft(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        MPJQueryWrapper<User> cemPsiUseMPJQueryWrapper = new MPJQueryWrapper<>();
        cemPsiUseMPJQueryWrapper.orderByDesc("id");
        cemPsiUseMPJQueryWrapper.selectAll(User.class);
        cemPsiUseMPJQueryWrapper.select("t.id,t.name,t1.balance");
        cemPsiUseMPJQueryWrapper.leftJoin("account t1 on t.account_id = t1.id");

        Page<UserVo> page2 = new Page<>(page, pageSize);

        IPage<UserVo> pageList = userRepository.selectJoinPage(page2, UserVo.class, cemPsiUseMPJQueryWrapper);

        return JSON.toJSONString(pageList);
    }
}
